package com.woniu.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.account.dto.AdminLoginDto;
import com.woniu.account.dto.PageAdminDTO;
import com.woniu.account.dto.UserPointDto;
import com.woniu.account.form.*;
import com.woniu.account.model.TAdminInfo;
import com.woniu.account.service.TAdminInfoService;
import com.woniu.core.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tAdminInfo")
@Api(tags = "管理员表相关接口")
public class TAdminInfoController {

    @Autowired
    private TAdminInfoService tAdminInfoService;
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private AliyunOSSUtil aliyunOSSUtil;

    @ApiOperation("管理员账号密码登录")
    @PostMapping("/adminLogin.admin")
    public Result adminLogin(@RequestBody @Valid LoginForm form) throws Exception {
        System.out.println("管理员账号密码登录");
        AdminLoginDto adminLoginDTO = tAdminInfoService.adminLogin(form);
        return Result.getResult(AdminLoginDto.class).setMsg("登录成功").setData(adminLoginDTO);
    }

    //发送登录验证码：
    @PostMapping("/sendLoginCode/{phone}")
    @ApiOperation("管理员发送登录验证码")
    //根据你的token里面带的角色动态生成菜单
    public Result sendLoginCode(@PathVariable String phone) throws Exception{
        System.out.println(phone);
        tAdminInfoService.sendLoginCode(phone);
        return Result.getResult(Boolean.class).setMsg("发送成功").setData(true);
    }
    @ApiOperation("管理员手机验证码登录")
    @PostMapping("/adminCodeLogin.admin")
    public Result adminCodeLogin(@RequestBody @Valid AdminInfoCodeLoginForm form) throws Exception{
        System.out.println("管理员手机验证码登录");
        AdminLoginDto adminLoginDTO = tAdminInfoService.adminCodeLogin(form);
        return Result.getResult(AdminLoginDto.class).setMsg("登录成功").setData(adminLoginDTO);
    }

    @ApiOperation("超管新建普通员工")
    @PostMapping("/addAdminAccount.admin")
    public Result addAdminAccount(@RequestBody @Valid AddAdminForm form) throws Exception{
        tAdminInfoService.addAdminAccount(form);
        return Result.getResult(Boolean.class).setMsg("新建成功").setData(true);
    }


    @ApiOperation("管理员修改密码")
    @PostMapping("/adminChangePassword.admin")
    public Result adminChangePassword(@RequestBody @Valid ChangePasswordForm form) throws Exception{
        tAdminInfoService.adminChangePassword(form);
        return Result.getResult(Boolean.class).setMsg("修改密码成功").setData(true);
    }


    @ApiOperation("修改教练账号状态和备注 1正常 0冻结")
    @PostMapping("/setCoachAccountState.admin")
    public Result setCoachAccountState(@RequestBody SetCoachStateForm form) throws Exception{
        tAdminInfoService.setCoachAccountState(form);
        return Result.getResult(Boolean.class).setMsg("修改成功").setData(true);
    }

    @ApiOperation("修改管理员账号状态和备注 1正常 0冻结")
    @PostMapping("/setAdminAccountState.admin")
    public Result setAdminAccountState(@RequestBody SetAccountStateForm form) throws Exception{
        tAdminInfoService.setAdminAccountState(form);
        return Result.getResult(Boolean.class).setMsg("修改成功").setData(true);
    }

    @ApiOperation("分页查询全部管理员或根据账户名或手机号查询账号信息")
    @GetMapping("/getAdminByCondition.admin")
    public Result getAdminByCondition(SearchAdminForm form) throws Exception{
        PageInfo<PageAdminDTO> pageInfo = tAdminInfoService.getAdminByCondition(form);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(pageInfo);
    }

    //上传图片
    @PostMapping("/upload")
    @ApiOperation("公用上传图片")
    public Result upload(MultipartFile image) throws Exception {
        String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String path = aliyunOSSUtil.uploadFile(image.getInputStream(), name + suffix, "ryusai");
        return Result.getResult(String.class).setMsg("上传成功").setData(path);
    }

    @PostMapping("/uploadImage.admin")
    @ApiOperation("管理员更改图片")
    //如果参数是MultipartFile来传参，就要分开；如果用String在参数对象就可以用参数对象而且要用@requestBody注解
    public Result uploadImage(MultipartFile image) throws Exception {
        Integer adminId = JwtUtil.getUserId(1);
        UpdateImageForm form = new UpdateImageForm();
        form.setImage(image);
        form.setAccountId(adminId);
        String s = tAdminInfoService.uploadImage(form);
        return Result.getResult(String.class).setMsg("修改成功").setData(s);
    }

    @GetMapping("/queryAdminNameById.admin/{adminId}")
    @ApiOperation("根据id查询管理员姓名")
    public Result<String> queryAdminNameById(@PathVariable("adminId") Integer adminId) throws Exception {
        String name = tAdminInfoService.queryAdminNameById(adminId);
        return Result.getResult(String.class).setMsg("根据id查询管理员姓名成功").setData(name);
    }

    @GetMapping("/queryAllUserPoint.admin")
    @ApiOperation("查询所有用户等级详情")
    public Result queryAllUserPoint(Integer pageNum,Integer pageSize) throws Exception {
        PageInfo<UserPointDto> pageInfo=tAdminInfoService.queryAllUserPoint(pageNum,pageSize);
        return Result.getResult(PageInfo.class).setMsg("查询所有用户等级详情").setData(pageInfo);
    }


}

